﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Author toanvt
/// CreatedDate 08/11/2011
/// </summary>
public partial class Account_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtUserName.Focus();
    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            MembershipUser theUser = Membership.GetUser(this.txtUserName.Text);

            if (theUser == null)
            {
                this.ltrMessage.Text = "Tài khoản chưa tồn tại!";
            }
            else
            {
                //tài khoản bị khóa
                if (theUser.IsLockedOut == true)
                {
                    this.ltrMessage.Text = "Tài khoản đã bị khóa";
                    return;
                }

                if (Membership.ValidateUser(this.txtUserName.Text, this.txtPassword.Text))
                {
                    if (Request.QueryString["ReturnUrl"] != null)
                    {
                        FormsAuthentication.RedirectFromLoginPage(this.txtUserName.Text, false);
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(theUser.UserName, false);
                        Response.Redirect("../Administrator/AdminIndex.aspx");
                    }
                }
                else
                {
                    this.ltrMessage.Text = "Mật khẩu không đúng";
                }
            }
        }
    }
}
